

$(function () {
   

    noname()

})




function keyword() {

   

    let keyword = $('#keyword').val()

    

    let obj = {
        keyword
    }
    
    noname(['find', obj])
}

function del(id) {

    let obj = {
        id
    }
   

    noname(['delete', obj])
}





function noname(arr) {
    

    $.get('http://localhost:8000/blog', (data) => {
        if (arr !== undefined) {
            $('.rowData').remove();

            if ((arr[0] == 'find' && arr[1] !== '') || arr[0] == 'add' || arr[0] == 'delete' || arr[0] === 'edit') {

                $.post(`http://localhost:8000/${arr[0]}`, arr[1], (res) => {

                    data = res


                    if (arr[0] === 'find') {
                        Data()
                    } else {
                        window.location.href = 'index.html'
                        Data()
                    }
                })
            }
        } else {
            Data()
        }

        function Data() {
            data.forEach(item => {
                let html = `
             <tr class='rowData'>
                <td> ${item.id} </td>
                <td> ${item.biaoti} </td>
                <td> ${item.zaiyao}</td>
                <td> ${item.neirong}</td>
                <td> ${item.writer}</td>
                <td> ${item.datatime}</td>


                <td> 
                <input type="button" value="删除" onclick="del(${item.id})">
                <input type="button" value="编辑" onclick="edit(${item.id})">
                      
                </td>

             </tr>
             `

                $('#tbData').append(html)
            });
        }
    })


}